#!/usr/bin/perl

do '../web-lib.pl';

&init_config();

#sub get_smb_list(\%smbcnt)
#%smbcnt = {dir, uid, gid, pid, machine, strip, time}
#return $n, $n is length of list
sub get_smb_list()
{
    my ($i, $j, @line1, @line2, @smbsts, @files, $smbcnt, $n, $pid, $dir);
    my (%smblockfile, $refilename, $fn, %sharename, %sharedir, $sn, $sd);

    $smbcnt = @_[0];

    &get_sn_sd(\%sharename, \%sharedir);

    #open(SMBSTS_S, "$config{'cmd_smbstatus'} -S|");
    #@smbsts = <SMBSTS_S>;
    #close(SMBSTS_S);

    @smbsts = `$config{'cmd_smbstatus'} -S 2>/dev/null`;

    #open(FILES, "$config{'cmd_smbstatus'} -L|");
    #@files = <FILES>;
    #close(FILES);

    @files = `$config{'cmd_smbstatus'} -p 2>/dev/null`;

    for($i = 0; $i < @files; $i++)
    {
	$files[$i] =~ s/#.*$//;
	
	if($files[$i] =~ /(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+\((.*)\)/)
	{
	    $pid = $1;
	    $refilename = $5;
            $ip{"$pid"}	= $5;
	    $user{"$pid"} = $2;
	    $sn = $sharename{"$1"};
	    print $sn; 
	    @line1 = split("\n", $sn);
	    for($j = 0; $j < @line1; $j++)
	    {
		$fn = $refilename;
		$sd = $sharedir{"$line1[$j]"};		
		if($fn =~ /^$sd/)
		{
		    $fn =~ s/^$sd/$line1[$j]/;
		    $smblockfile{"$pid"} = $smblockfile{"$pid"}.$fn."\n";
		}
	    }
	}
    }
    
    @files_l = `$config{'cmd_smbstatus'} -L 2>/dev/null`;

    for($i = 0; $i < @files_l; $i++)
    {
        $files_l[$i] =~ s/#.*$//;

        if($files_l[$i] =~ /(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/)
        {
            $pid = $1;
            $refilename = $6;

            $sn = $sharename{"$1"}; 
	    `echo $n" "$refilename > /root/pppc`;
	    $smblockfile{"$pid"} .= $refilename;
            @line1 = split("\n", $sn);
            for($j = 0; $j < @line1; $j++)
            {
                $fn = $refilename;
                $sd = $sharedir{"$line1[$j]"};
                if($fn =~ /^$sd/)
                {
                    $fn =~ s/^$sd/$line1[$j]/;
                    $smblockfile{"$pid"} = $smblockfile{"$pid"}.$fn."\n";
                }
            }
        }
    }
    #print @smbsts;

    $n = 0;
    for($i = 0; $i < @smbsts; $i++)
    {
	$smbsts[$i] =~ s/#.*$//;

	if($smbsts[$i] =~ /(\S+)\s+(\d+)\s+(\S+)\s+(.*)/)
	{
	    if($1 eq "IPC\$")
	    {
		next;
	    }
	    $smbcnt->{$n.'^dir'} = $1;
	    $smbcnt->{$n.'^uid'} = $user{"$2"};
	    $smbcnt->{$n.'^gid'} = $3;
	    $smbcnt->{$n.'^pid'} = $2;
	    $smbcnt->{$n.'^machine'} = $3;
	    $smbcnt->{$n.'^strip'} = $ip{"$2"};
	    $smbcnt->{$n.'^time'} = $4;
	    $dir = $1;
	    $fn = $smblockfile{"$2"};
            `echo $2" "$fn >> /root/pppc`;	
	    @line1 = split("\n", $fn);
	    #$fn = "";
	    for($j = 0; $j < @line1; $j++)
	    {
		if($line1[$j] =~ /^$dir/)
		{
		    $fn = $fn.$line1[$j]."\n";		
		}
	    }
	    if($fn)
	    {
		 $smbcnt->{$n.'^lockfiles'} = $fn;
	    }
	    $n++;
	}
    }
    return $n;
}

#ab2re_file($filename, $pid)
#absolute file name change to relatively file name by sharetab
#return $refilename
sub ab2re_file()
{
	my ($filename, $pid, @sharetab, @line, $i, %sharedir, %sharename, $refilename);
	my ($sn, $sd);

	$filename = @_[0];
	$pid = @_[1];

	open(SHARETAB, "$config{'sharetab'}");
	@sharetab = <SHARETAB>;
	close(SHARETAB);

	#open(SMBSTATUS, "$config{'cmd_smbstatus'} -S|");
	#@smbstatus = <SMBSTATUS>;
	#close(SMBSTATUS);
	
	@smbstatus = `$config{'cmd_smbstatus'} -S 2>/dev/null`;

	for($i = 0; $i < @sharetab; $i++)
	{
		$sharetab[$i] =~ s/#.*$//;
		chomp($sharetab[$i]);
		@line = split(":", $sharetab[$i]);
		$sharedir{"$line[1]"} = $line[0];

	}

	for($i = 0; $i < @smbstatus; $i++)
	{
		$smbstatus[$i] =~ s/#.*$//;
		
		if($smbstatus[$i] =~ /(\S+)\s+\"(.*)\"\s+\"(.*)\"\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/)
		{
			if($1 eq "IPC\$")
			{
				next;
			}

			$sharename{"$4"} = $1;
		}
	}

	$sn = $sharename{"$pid"};
	$sd = $sharedir{"$sn"};
	$filename =~ s/^$sd/$sn/;
	return $filename;
}

#get_sn_sd(\%sharename, \%sharedir)
sub get_sn_sd()
{
	my($sharename, $sharedir);
	my ($pid, @sharetab, @line, $i);

	$sharename = @_[0];
	$sharedir = @_[1];

	open(SHARETAB, "$config{'sharetab'}");
	@sharetab = <SHARETAB>;
	close(SHARETAB);

	#open(SMBSTATUS, "$config{'cmd_smbstatus'} -S|");
	#@smbstatus = <SMBSTATUS>;
	#close(SMBSTATUS);

	@smbstatus = `$config{'cmd_smbstatus'} -S 2>/dev/null`;

	for($i = 0; $i < @sharetab; $i++)
	{
		$sharetab[$i] =~ s/#.*$//;
		chomp($sharetab[$i]);
		@line = split(":", $sharetab[$i]);
		$sharedir->{"$line[1]"} = $line[0];

	}

	for($i = 0; $i < @smbstatus; $i++)
	{
		$smbstatus[$i] =~ s/#.*$//;
		
		if($smbstatus[$i] =~ /(\S+)\s+\"(.*)\"\s+\"(.*)\"\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/)
		{
			if($1 eq "IPC\$")
			{
				next;
			}

			$sharename->{"$4"} = $sharename->{"$4"}.$1."\n";
		}
	}
}

#check_smbproc($pid)
sub check_smbproc()
{
	my ($pid, @ps_a, $i);

	$pid = @_[0];
	@ps_a = `ps -A 2>/dev/null`;

	for($i = 0; $i < @ps_a; $i++)
	{
		if($ps_a[$i] =~ /$pid/)
		{
			if($ps_a[$i] =~ /smbd/)
			{
				return 1;
			}
			else
			{
				last;
			}
		}
	}
	
	return 0;
}

sub check_pid()
{
	my $pid, @smbstats, $i, $p;

	$pid = $_[0];

	@files = `$config{'cmd_smbstatus'} -S 2>/dev/null`;

	for($i = 0; $i < @files; $i++)
	{
		if($files[$i] =~ /(\S+)\s+\"(.*)\"\s+\"(.*)\"\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/)
		{
			$p = $4;
			if($pid eq $p)
			{
				return 1;
			}
		}	
	}
	
	return 0;
}

1;
